from random import randint, choices, shuffle
from string import ascii_uppercase, ascii_lowercase
from Day11.nico import builder as random_phone


def random_interval(lenght=None, _safe=1):
    # 确定生成密码的总长度
    total_len = lenght if lenght else randint(8, 16)

    # 确定大小写及下划线密码子串长度
    sub = randint(3, total_len)
    up = randint(1, sub - 2)
    low = randint(1, sub - up - 1)
    underline = _safe if (sub - up - low) <= _safe else _safe
    return up, low, underline, total_len - up - low - underline


# 随机生成密码
def random_password(lenght=None, _safe=1):
    '''
    :param lenght: 需要生成密码长度
    :param _safe: 限制下划线数量 避免下划线太多
    :return:
    '''
    up, low, underline, n = random_interval(lenght=lenght, _safe=_safe)
    pw = choices(ascii_uppercase, k=up) + choices(ascii_lowercase, k=low) + underline * ['_'] + choices('0123456789', k=n)
    shuffle(pw)
    return ''.join(pw)


# 随机生成账号信息
def v_accounts(n):
    accounts = {}
    while True:
        # 达到指定数量,跳出循环
        if len(accounts) >= n:
            break
        phone = random_phone()
        # 判断号码是否重复
        if phone in accounts:
            continue

        password = random_password()
        accounts[phone] = password
    return [{'phone': k, 'password': v} for k, v in accounts.items()]


if __name__ == '__main__':
    print(v_accounts(2))
